﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace PlatformClient
{
    public partial class ChangePassWordForm : DevExpress.XtraEditors.XtraForm
    {
        public ChangePassWordForm()
        {
            InitializeComponent();
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            if(textEdit_oldPwd.Text.Trim()=="")
            {
                XtraMessageBox.Show("请输入原密码！");
                textEdit_oldPwd.Focus();
                return;
            }
            if(textEdit_newPwd.Text.Trim()=="")
            {
                XtraMessageBox.Show("请输入新密码！");
                textEdit_newPwd.Focus();
                return;
            }
            if(textEdit_newPwd2.Text.Trim()=="")
            {
                XtraMessageBox.Show("请重复新密码！");
                textEdit_newPwd2.Focus();
                return;
            }            
            if(textEdit_newPwd.Text != textEdit_newPwd2.Text)
            {
                XtraMessageBox.Show("两次输入新密码不一致，请核对！");
                textEdit_newPwd2.Focus();
                return;
            }
            if (Encoding.GetEncoding("GBK").GetBytes(textEdit_newPwd.Text).Length > 200)
            {
                XtraMessageBox.Show("新密码长度过长，请减少密码长度！");
                textEdit_newPwd.Focus();
                return;
            }
            //验证身份
            if(!MainForm.PositionService.CheckUser(MainForm.MyUserInfo.UserName,textEdit_oldPwd.Text))
            {
                XtraMessageBox.Show("原密码(即当前密码)输入有误，请重试！");
                textEdit_oldPwd.Focus();
                return;
            }
            //修改密码
            if(MainForm.PositionService.ChangePassWordByUserName(MainForm.MyUserInfo.UserName,
                textEdit_newPwd.Text))
            {
                XtraMessageBox.Show("密码修改成功！");
                textEdit_newPwd.Text = "";
                textEdit_newPwd2.Text = "";
                textEdit_oldPwd.Text = "";
            }
            else
            {
                XtraMessageBox.Show("密码修改失败！");
            }
        }

        #region 窗体控件效果
        Point _mouseOff;
        bool _leftFlag;
        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _mouseOff = new Point(-e.X, -e.Y);
                _leftFlag = true;
            }
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(_mouseOff.X, _mouseOff.Y);
                Location = mouseSet;
            }
        }

        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                _leftFlag = false;
            }
        }

        private void pictureEdit_exit_MouseHover(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexit;
            Cursor = Cursors.Hand;
        }

        private void pictureEdit_exit_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexitPressDown;

            Cursor = Cursors.Arrow;

        }
        private void btn_MouseHover(object sender, EventArgs e)
        {
            Cursor = Cursors.Hand;
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void pictureEdit_exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void  Form_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Close();
            }
        }
        #endregion

        private void textEdit_newPwd2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //e.Handled = true;    
                btn_Save_Click(sender, e);
            }
        }
    }
}